﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class caprogram2
    {
        static ProvinceTestDataContext DB = new ProvinceTestDataContext();
        static void Main(string[] args)
        {
            if (File.Exists("City_1.txt"))
            {
                List<Province> listProvince = new List<Province>();
                using (StreamReader sr = new StreamReader("City_1.txt", System.Text.Encoding.Default))
                {
                    string readall = sr.ReadToEnd().TrimEnd('|');
                    String[] oneLines = readall.Split('|');
                    for (int i = 0; i < oneLines.Length; i++)
                    {
                        String provinceID = oneLines[i].Split(':')[0];
                        String provinceName = oneLines[i].Split(':')[1];
                        Province province = new Province()
                        {
                            ProvinceID = Int16.Parse(provinceID),
                            ProvinceName = provinceName
                        };

                        listProvince.Add(province);
                    }
                }
                foreach (var item in listProvince)
                {
                    Console.WriteLine(item.ProvinceID + ":" + item.ProvinceName);
                }
                DB.Province.InsertAllOnSubmit(listProvince);
            }

            if (File.Exists("City_2.txt"))
            {
                List<City> listCity = new List<City>();
                using (StreamReader sr = new StreamReader("City_2.txt", System.Text.Encoding.Default))
                {
                    string readall = sr.ReadToEnd().TrimEnd('|');
                    String[] oneLines = readall.Split('|');
                    for (int i = 0; i < oneLines.Length; i++)
                    {
                        String provinceID = oneLines[i].Split(':')[1];
                        String cityID = oneLines[i].Split(':')[2];
                        String cityName = oneLines[i].Split(':')[3];
                        City city = new City()
                        {
                            CityID = Int32.Parse(cityID),
                            CityName = cityName,
                            provinceID = Int16.Parse(provinceID)
                        };
                        listCity.Add(city);
                    }
                    foreach (var item in listCity)
                    {
                        Console.WriteLine(item.CityID + ":" + item.CityName + "-->" + item.provinceID);
                    }
                    DB.City.InsertAllOnSubmit(listCity);
                }
            }
            Console.WriteLine("OK!");
            //DB.SubmitChanges();
            Console.ReadKey();
        }

    }
}
